package com.qm.console.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.model.SysPageComponent;
import com.qm.console.model.SysRequest;
import com.qm.console.model.SysRequestWithBLOBs;
import com.qm.console.model.SysRole;
import com.qm.console.model.UserInfo;
import com.qm.console.service.PageService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.controller.rest
 * Created on 2021/7/14.
 */
@RestController
@RequestMapping("/page")
public class PageController {

    @Autowired
    private PageService sysRoleService;
    /**
     * 查询页面组件列表
     *
     */
    @RequestMapping(value = "/pageComponent", method = RequestMethod.GET)
    public BaseRsp pageComponent(@RequestParam(value="pageId", required=true) int pageId){
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "list", sysRoleService.getPageComponent(pageId)));
    }

    /**
     * 写入或更新组件列表
     *
     */
    @RequestMapping(value = "/installComponent", method = RequestMethod.POST)
    public BaseRsp installComponent(@RequestBody SysPageComponent record){
        sysRoleService.insertUpdateComponent(record);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of());
    }

    /**
     * 删除组件
     *
     */
    @RequestMapping(value = "/deleteComponent", method = RequestMethod.POST)
    public BaseRsp deleteComponent(@RequestBody JSONObject params){
        int componentId = params.getIntValue("componentId");
        sysRoleService.deleteComponent(componentId);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of());
    }

    /**
     * 添加接口模板
     *
     */
    @RequestMapping(value = "/addRequest", method = RequestMethod.POST)
    public BaseRsp addRequest(@RequestBody SysRequestWithBLOBs params){
        sysRoleService.addRequest(params);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of());
    }

    /**
     * 查询接口列表
     *
     */
    @RequestMapping(value = "/request/list", method = RequestMethod.GET)
    public BaseRsp requestList(){
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "list", sysRoleService.requestsList()
        ));
    }

    @RequestMapping(value = "/user/can/edit", method = RequestMethod.GET)
    public BaseRsp userCanEdit() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        List<SysRole> roleList = activeUser.getSysRoleList();
        Boolean result = roleList.stream().anyMatch(item -> item.getId() == 1);
        return new BaseRsp(ResultEnum.SUCCESS, result);
    }

}
